﻿using System;
using System.Data;
using System.Data.DBLib;
using System.Web;
using SysComm;

namespace SysOperationLayer.DBHandle
{
    public class GroupsDB : DBBase
    {
        public GroupsDB()
        {
        }

        public GroupsDB(CSqlDB objDB)
        {
            m_objDB = objDB;
        }

        public DataTable getUserGroupList(string strGroupName)
        {
            if (ConnectDB())
            {
                string strSql = "select * from DBF_USER_GROUP";

                if (!IsEmpty(strGroupName))
                {
                    strSql += " where GroupName like '%" + strGroupName + "%' ";
                }

                return m_objDB.getDataTable(strSql, "DBF_USER_GROUP");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public DataRow getUserGroupById(string rowno)
        {
            if (ConnectDB())
            {
                string strSql = "select * from DBF_USER_GROUP where rowno = " + rowno;
                return m_objDB.getDataRow(strSql, "DBF_USER_GROUP");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        public int Insert(string strGroupName, string strRemark)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strUserId = HttpContext.Current.Session[SysUtil.m_str_session_username].ToString();
                string strUserIp = HttpContext.Current.Request.UserHostAddress;
                string strSql = "insert into DBF_USER_GROUP(GroupName, GroupType, Remark , CreateIp, CreateBy, CreateDate, UpdateIp, UpdateBy, UpdateDate) values(N'";
                strSql += strGroupName;
                strSql += "','Normal', N'";
                strSql += strRemark;
                strSql += "', '";
                strSql += strUserIp;
                strSql += "', '";
                strSql += strUserId;
                strSql += "', getDate(), '";
                strSql += strUserIp;
                strSql += "', '";
                strSql += strUserId;
                strSql += "', getDate())";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;

                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Update(string strRowno, string strGroupName, string strRemark)
        {
            if (IsEmpty(strRowno))
            {
                m_strMessage = "Record Not Found!";
            }
            else if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strUserId = HttpContext.Current.Session[SysUtil.m_str_session_username].ToString();
                string strUserIp = HttpContext.Current.Request.UserHostAddress;
                string strSql = "update DBF_USER_GROUP set ";
                strSql += " GroupName = N'";
                strSql += strGroupName;
                strSql += "', Remark = N'";
                strSql += strRemark;
                strSql += "', UpdateBy = N'";
                strSql += strUserId;
                strSql += "', UpdateIp = '";
                strSql += strUserIp;
                strSql += "', UpdateDate = getdate()";
                strSql += " where Rowno = ";
                strSql += strRowno;
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int Trash(string rowno)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "delete from DBF_USER_GROUP where Rowno=" + rowno;
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }
    }
}
